package com.njtoyo.taxi.admin.library.traffic_info;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class TrafficInfoQueryClient {
    private final String from = "1";
    private String key;
    private String requestTime;
    private String sign;
    private String access_token;
    private Param param;
    private String referer;

    public TrafficInfoQueryClient setSign(String sign) {
        this.requestTime = String.valueOf(System.currentTimeMillis());
        this.sign = SecureUtil.md5(sign + requestTime);
        return this;
    }

    @Override
    public String toString() {
        String referer = this.referer;
        this.referer = null;
        String param = JSONUtil.toJsonStr(this);
        this.referer = referer;
        return param;
    }

    public Object send() {
        String res = HttpRequest.post("http://app.gjzwfw.gov.cn/jimps/link.do")
                .header(Header.REFERER, this.referer)
                .header(Header.USER_AGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0")
                .form("param", this.toString())
                .execute().body();
        System.out.println(res);
        return JSONObject.parseObject(res);
    }
}
